package com.hongpu.config;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.event.EventListener;
import org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent;
import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class AuthEventListener {
    @EventListener
    public void onAuthenticationSuccess(final AuthenticationSuccessEvent event) {
        String username = event.getAuthentication().getName();
        log.info(String.format("登录认证成功事件发布信息:用户{%s}登录成功 ",username));
        // 记录日志、发送通知等
    }

    @EventListener
    public void onAuthenticationFailure(final AbstractAuthenticationFailureEvent event) {
        String username = event.getAuthentication().getName();
        log.info(String.format("登录认证失败事件发布信息:用户{%s}登录失败, 原因是: {%s}",username,event.getException().getMessage()));
        // 记录失败尝试、触发告警等
    }
}